package com.adinnet.admin.organization.service.impl;

import com.adinnet.admin.classs.mapper.ClasssMapper;
import com.adinnet.admin.classs.model.Classs;
import com.adinnet.admin.organization.model.Organization;
import com.adinnet.admin.organization.mapper.OrganizationMapper;
import com.adinnet.admin.organization.model.vo.OrganizationResp;
import com.adinnet.admin.organization.service.OrganizationService;
import com.adinnet.admin.school.mapper.SchoolMapper;
import com.adinnet.admin.school.model.School;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author liukp
 * @since 2020-12-05
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private ClasssMapper classsMapper;

    @Autowired
    private OrganizationMapper organizationMapper;


    /**
     * 根据权限查询机构列表
     *
     * 超级管理员查询所有机构，其余角色查询所在机构
     * @param sysUserInfo
     * @return
     */
    @Override
    public List<Organization> getListByUserRole(SysUserInfo sysUserInfo){
        QueryWrapper queryWrapper=new QueryWrapper();
        if(!RoleCode.ADMIN.getCode().equals(sysUserInfo.getMaxRole())){
            queryWrapper.eq("id",sysUserInfo.getOrganizationId());
        }
        queryWrapper.eq("deleted",0);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * @param keyword:机构名称
     * @Description: 查询所有机构
     * @author zYshuai
     * @Date: 14:49
     * @return: java.util.List<com.adinnet.admin.organization.model.vo.OrganizationResp>
     */
    @Override
    public PageResult<OrganizationResp> organizationList(String keyword, Integer page, Integer limit) {
        Page<OrganizationResp> objectPage = new Page<>(page, limit);
        List<OrganizationResp> organizationList = baseMapper.organizationList(objectPage,keyword);
        Integer classNum=0;
        if(CollectionUtils.isNotEmpty(organizationList)){
            for (OrganizationResp organizationResp : organizationList) {
                 if(StringUtils.isNotEmpty(organizationResp.getSchoolIds())){
                     List<String> ids = Arrays.asList(organizationResp.getSchoolIds().split(","));
                     classNum  = classsMapper.selectCount(new QueryWrapper<Classs>().in("school_id", ids).eq("deleted",0));
                     organizationResp.setClassNum(classNum);
                 }else {
                     organizationResp.setClassNum(0);
                 }
            }
        }
        return new PageResult<>(organizationList, objectPage.getTotal());
    }


    /**
     * @param organizationId:
     * @Description: 删除机构
     * @author zYshuai
     * @Date: 2020/12/10 14:39
     * @return: void
     */
    @Override
    public JsonResult delete(Integer organizationId) {
        //判断机构下 校区数和班级数是否为0
        Organization organization = baseMapper.selectById(organizationId);
        if (null == organization) {
            return JsonResult.error("查询机构失败");
        }
        QueryWrapper<School> wrapper = new QueryWrapper<School>().eq("organization_id", organization.getId()).eq("deleted", 0);
        List<School> schools = schoolMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(schools)) {
            return JsonResult.error("机构内有" + schools.size() + "个校区，移除校区后才可删除机构。");
        }
        organization.setDeleted(true);
        baseMapper.updateById(organization);
        return JsonResult.ok("删除成功");
    }


}
